package me.fanchaoo.web.service.impl;

import io.lettuce.core.dynamic.annotation.Param;
import lombok.extern.slf4j.Slf4j;
import me.fanchaoo.dbo.TagDBO;
import me.fanchaoo.exception.BusinessException;
import me.fanchaoo.util.BeanCopyUtils;
import me.fanchaoo.util.PageUtils;
import me.fanchaoo.util.UserContext;
import me.fanchaoo.web.dao.mapper.TagMapper;
import me.fanchaoo.web.dao.mapper.TagQueryMapper;
import me.fanchaoo.web.dto.LoginUserDTO;
import me.fanchaoo.web.dto.MomentDTO;
import me.fanchaoo.web.request.AddTagRequest;
import me.fanchaoo.web.request.QueryTagRequest;
import me.fanchaoo.web.response.BaseResponse;
import me.fanchaoo.web.service.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class TagServiceImpl implements TagService {


    @Resource
    private TagMapper tagMapper;
    @Resource
    private TagQueryMapper tagQueryMapper;

    @Override
    public BaseResponse<TagDBO> addTag(AddTagRequest request) {
        BaseResponse<TagDBO > response = new BaseResponse<>();
        LoginUserDTO loginUser = UserContext.getLoginUser();
        Long artistId = loginUser.getArtistId();
        Long userId = loginUser.getUserId();
        TagDBO  tagDBO =  tagQueryMapper.selectByName(request.getName());

        if (!Objects.isNull(tagDBO)) {
            throw new BusinessException("该标签已存在");
        }
        TagDBO  dbo = BeanCopyUtils.copy(request, TagDBO.class);
        dbo.setArtistId(artistId);
        dbo.setCreateUserId(userId);
        tagMapper.insertSelective(dbo);
        TagDBO dto=  tagQueryMapper.selectByName(request.getName());
        response.setBody(dto);
        return response;
    }
 
    public BaseResponse<List<TagDBO >>  queryTag(QueryTagRequest request) {
        BaseResponse<List<TagDBO>> response = new BaseResponse<>();
        LoginUserDTO loginUser = UserContext.getLoginUser();
        Long userId = loginUser.getUserId();
        Long artistId = loginUser.getArtistId();
        Integer pageNo = request.getPageNo();
        Integer pageSize = request.getPageSize();
        String keyword= request.getKeyword();
        String sort = request.getSort ();
        int start = PageUtils.getStart(pageNo, pageSize);
        List<TagDBO> TagDBOList =  tagQueryMapper.queryTag(artistId, null, start,pageSize,keyword,sort);
        response.setBody(TagDBOList);
        return response;

    }

    
}
